/**
 * Copyright (c) 2009 blurredVision
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
 * associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or 
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 
 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT 
 * OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#include "StdAfx.h"
#include "IMeshGeometry.h"

namespace model {

IMeshGeometry::IMeshGeometry(void)
:	m_uiNumFaces(0), 
	m_pFaces(NULL),
	m_uiNumVertices(0), 
	m_pVertices(NULL),
	m_pUniqueVertices(NULL),
	m_uiNumUniqueVertices(0),
	m_pIndicesInverted(NULL),
	lastLOD(LODLEVEL-1)
{
}

IMeshGeometry::~IMeshGeometry(void)
{
	DELETE_ARRAY(m_pVertices);
	DELETE_ARRAY(m_pFaces);
	DELETE_ARRAY(m_pUniqueVertices);
	DELETE_ARRAY(m_pIndicesInverted);	
}

const unsigned int IMeshGeometry::getNumVertices() const
{
	return m_uiNumVertices;
}

const unsigned int IMeshGeometry::getNumFaces() const
{
	return m_uiNumFaces;
}

sMeshVertex* IMeshGeometry::getMeshVertices()
{
	return m_pVertices;
}

sMeshFace* IMeshGeometry::getMeshFaces()
{
	return m_pFaces;
}

unsigned int* IMeshGeometry::getInvertedIndices()
{
	if(!m_pIndicesInverted){
		m_pIndicesInverted = new unsigned int[m_uiNumFaces*3];

		for(unsigned int i=0; i<m_uiNumFaces; ++i) {			
			// Reversed indices			
			
			m_pIndicesInverted[(i*3)] = m_pFaces[(m_uiNumFaces-1)-i].vertexIndex[2];
			m_pIndicesInverted[(i*3)+1] = m_pFaces[(m_uiNumFaces-1)-i].vertexIndex[1];
			m_pIndicesInverted[(i*3)+2] = m_pFaces[(m_uiNumFaces-1)-i].vertexIndex[0];
			
			/*
			m_pIndicesInverted[(i*3)] = m_pFaces[i].vertexIndex[0];
			m_pIndicesInverted[(i*3)+1] = m_pFaces[i].vertexIndex[1];
			m_pIndicesInverted[(i*3)+2] = m_pFaces[i].vertexIndex[2];*/
			
		}
	}

	return m_pIndicesInverted;
}

bool IMeshGeometry::load(FILE *file)
{
	// Read the number of vertices
	fread(&m_uiNumUniqueVertices, sizeof(unsigned int), 1, file);

	// Read the raw vertex data
	m_pUniqueVertices = (math::Vector3*) malloc(sizeof(math::Vector3)*m_uiNumUniqueVertices);
	fread(m_pUniqueVertices, sizeof(math::Vector3), m_uiNumUniqueVertices, file);

	// Read the number of faces
	fread(&m_uiNumFaces, sizeof(unsigned int), 1, file);

	// Read the raw face data
	m_pFaces = (sMeshFace*) malloc(sizeof(sMeshFace)*m_uiNumFaces);
	fread(m_pFaces, sizeof(sMeshFace), m_uiNumFaces, file);

	// READING IS DONE. 

	// CONVERT TO sMeshVertex
	m_uiNumVertices = m_uiNumFaces * 3;

	// Allocate memory for the mesh vertices
	m_pVertices = (sMeshVertex*) malloc(sizeof(sMeshVertex)*m_uiNumVertices);
	memset( m_pVertices, 0, sizeof(sMeshVertex)*m_uiNumVertices);

	int counter = 0;
	for (unsigned int i=0; i<m_uiNumFaces; ++i) {
		for (int j=0; j<3; ++j) {
			// Get the index of the face corner
			int index = m_pFaces[i].vertexIndex[2-j];
			// Set position, normal and tex0
			m_pVertices[counter+j].position = m_pUniqueVertices[ index ];
			m_pVertices[counter+j].normal   = m_pFaces[i].normals[2-j];
			m_pVertices[counter+j].tex0     = m_pFaces[i].texCoord0[2-j];
		}
		counter += 3;
	}

	unsigned int lodslevel = m_uiNumVertices / LODLEVEL;
	m_uiNumLODVertices[0] = lodslevel;
	for (int i=1; i<LODLEVEL-1; ++i)
		m_uiNumLODVertices[i] = lodslevel * (i+1);
	m_uiNumLODVertices[LODLEVEL-1] = m_uiNumVertices;

	// Initialize the mesh data (renderer specific)
	return initMesh();
}

} // namespace
